<?php


namespace app\model;


use think\facade\Db;
use think\Model;
use app\model\RemarkModel;
use app\model\ConfigModel;
class AfterSalesHandleModel extends Model
{
    protected $table = "store_after_sales_handle";

    /**
     * @param $param
     * @return AfterSalesHandleModel|array|mixed|string|Model|null
     * @notes 提交处理情况
     * @author: dongdong
     * @date: 2025/1/13 12:10
     */
    public function submit_Handle($param)
    {
        Db::startTrans();

        try {
            $data['after_sales_id'] = $param['after_sales_id']??'';
            $data['user_id'] = $param['user_id']??'';
            $data['handle_type'] = $param['handle_type']??'';
            $data['handle_money'] = $param['handle_money']??0;
            $data['user_type'] = $param['user_type']??'store';
            $data['create_at'] = date('Y-m-d H:i:s');

            $insertGetId = self::insertGetId($data);

            $handle_content = $param['handle_content'];
            $RemarkModel = new RemarkModel();
            $handle_content_id = 0;
            if (!empty($handle_content)) {
                $handle_content_id = $RemarkModel::remark_id($handle_content, $this->table, $insertGetId);
            }
            self::where(["id" => $insertGetId])->update(['handle_content_id' => $handle_content_id]);

            Db::commit();
            return self::find([$insertGetId]);
        } catch (\Exception $e) {
            Db::rollback();
            return '';
        }
    }

    /**
     * @param $id
     * @return AfterSalesHandleModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes  处理列表
     * @author: dongdong
     * @date: 2025/1/13 13:54
     */
    public function lists($id){
        $RemarkModel = new RemarkModel();
        $ConfigModel = new ConfigModel();
        $lists = self::where(['after_sales_id'=>$id])->select();
        foreach ($lists as $k =>&$v){
            $v['handle_config'] = $ConfigModel->info($v['handle_type']);
            $v['handle_content'] = $RemarkModel->info($v['handle_content_id']);
        }
        return $lists;
    }
}